define(['backbone'], function (Backbone) {
	var Router = Backbone.Router.extend({
		routes: {
			'': 'home',
			'home': 'home',
			'market': 'market',
			'shoppingCart': 'shoppingCart',
			'mine': 'mine'
		},
		home: function () {
			require(['jquery', 'text!./home/home.html', 'css!./home/css/home.css', 'homejs'], function ($, html, css, homejs) {
				$('#wrap').html(html);
				homejs.homeAjax();
			});
		},
		market: function () {
			require(['jquery', 'text!./market/market.html', 'css!./market/css/market.css', 'marketjs'], function ($, html, css, marketjs) {
				$('#wrap').html(html);
				marketjs.marketAjax();
			});
		},
		shoppingCart: function () {
			require(['cookie', 'shoppingjs'], function (cookie, shoppingjs) {
				// ifLog(cookie, './shoppingCart/shoppingCart.html', './shoppingCart/css/shoppingCart.css');
				var username = cookie.getCookie('username');
				var password = cookie.getCookie('password');

				if (username == '18310148117' && password == '123456') {
					// 存在正确用户名密码，发送已登录页面
					require(['jquery', 'text!./shoppingCart/shoppingCart.html', 'css!./shoppingCart/css/shoppingCart.css', 'shoppingjs'], function ($, html, css, shoppingjs) {
						$('#wrap').html(html);
						// 调用shoppingjs的方法
						setTimeout(function () {
							shoppingjs.minus();
							shoppingjs.add();
						}, 100)
					});
				} else {
					// 否则发送未登录页面
					require(['jquery', 'text!./login/login.html', 'css!./login/css/login.css', 'loginjs'], function ($, html, css, loginjs) {
						$('#wrap').html(html);
						loginjs.login();
					});
				}
			});
		},
		mine: function () {
			require(['cookie', 'minejs'], function (cookie, minejs) {
				// ifLog(cookie, './mine/mine.html', './mine/css/mine.css', minejs);
				var username = cookie.getCookie('username');
				var password = cookie.getCookie('password');
				if (username == '18310148117' && password == '123456') {
					require(['jquery', 'text!./mine/mine.html', 'css!./mine/css/mine.css', 'minejs'], function ($, html, css, minejs) {
						$('#wrap').html(html);
						minejs.logOut();
					});
				} else {
					require(['jquery', 'text!./login/login.html', 'css!./login/css/login.css', 'loginjs'], function ($, html, css, loginjs) {
						$('#wrap').html(html);
						loginjs.login();
					});
				}
			});
		}
	});
	var router = new Router();
	Backbone.history.start();

	// 封装判断cookie的函数
	/*function ifLog(cookie, htmlPath, cssPath, minejs) {
		var username = cookie.getCookie('username');
		var password = cookie.getCookie('password');
		// console.log(username + '-->' + password);
		// 根据cookie判断发送哪个页面
		if (username == '18310148117' && password == '123456') {
			// 存在正确用户名密码，发送已登录页面
			require(['jquery', 'text!' + htmlPath, 'css!' + cssPath, 'minejs'], function ($, html, css, minejs) {
				$('#wrap').html(html);

				// 也可以使用判断传入路径的方式避免报错。如果是购物车模块调用此方法，则不调用minejs.logOut();方法
				try {
					minejs.logOut();
				} catch (e) {
					// 此报错不影响程序运行
					// console.log(e);
				};
			});
		} else {
			// 否则发送未登录页面
			require(['jquery', 'text!./login/login.html', 'css!./login/css/login.css', 'loginjs'], function ($, html, css, loginjs) {
				$('#wrap').html(html);
				loginjs.login();
			});
		}
	}*/
});